﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Fcl.WSOP;


namespace LicPdaService
{
   public class PDAXmlPrePickProcess
    {
        /// <summary>
        /// 3.0 空箱扫描库存 Add By Quinn.Ling 201705
        /// DataSource:Inventory
        /// </summary>
        public static string GetPDAPrePickData(XmlDocument doc)
        {
            #region 通用变量定义
            string docResult = string.Empty; //返回值
            string userid = string.Empty;
            string whseid = string.Empty;
            string master_label = string.Empty;
            string sku = string.Empty;
            string label_qty = string.Empty;
            string qty ="0";
            string return_status = string.Empty;
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            string batch = string.Empty;

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点
            #endregion

            try
            {
                #region 参数源
                App.DBconSet();
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNode nodeSku = root.SelectSingleNode("sku");
                XmlNodeList xmlnodelist = nodeSku.ChildNodes;
                try
                {
                    userid = nodeSku.SelectSingleNode("userid").InnerText;
                    whseid = nodeSku.SelectSingleNode("whseid").InnerText;
                    master_label = nodeSku.SelectSingleNode("master_label").InnerText;
                    try
                    {
                        string[] s = master_label.Split(new char[] { '|' });
                        if (s.Length > 2)
                        {
                            sku = s[0].ToString();
                            label_qty = s[3].ToString();
                            batch = s[2].ToString();
                        }
                        else
                        {
                            sku = master_label;
                        }

                    }
                    catch( Exception ex) {
                        App.Log("PDAXmlPrePickProcess.cs-2", ex.Message.ToString());
                    }

                    //库存量判定
                    strSql = "SELECT Inventory.sku,cast(SUM(Inventory.qty-Inventory.allocated_qty-Inventory.picked_qty) as float) AS qty FROM Inventory ";
                    strSql += " left join LotAttribute on Inventory.lot=LotAttribute.lot  where Inventory.sku='" + sku + "'  ";
                    strSql += "  and Inventory.whseid='" + whseid + "' GROUP BY Inventory.sku";
                    DataTable dt = App.GetDataTable(App.DBconWMS, strSql, "Inventory");
                    if (dt.Rows.Count > 0)
                    {
                        qty = dt.Rows[0]["qty"].ToString();
                        return_status = "";
                    }
                    else
                    {
                        return_status = "数据库中不存在对应sku库存数据";
                    }

                    //料号有效性判定
                    strSql = "SELECT whseid,customer_code,sku FROM Sku where sku='" + sku + "' and  customer_code like 'MGNYCL%'";
                    DataTable dtsku = App.GetDataTable(App.DBconWMS, strSql, "sku");
                    if (dtsku.Rows.Count > 0)
                    {
                        return_status = "";
                    }
                    else
                    {
                        return_status = "料号："+sku+"无效（原因：料号不存在或料号对应货主不是麦格纳原材料），加入列表失败";
                    }
                }
                catch( Exception ex) {
                    App.Log("PDAXmlPrePickProcess.cs-3", ex.Message.ToString());
                }

                #endregion

                #region 构建XML报文
                XmlNode sku_result = docreturn.CreateElement("sku_return");

                XmlElement useridResult = docreturn.CreateElement("userid");
                useridResult.InnerText = userid;
                sku_result.AppendChild(useridResult);

                XmlElement whseidResult= docreturn.CreateElement("whseid");
                whseidResult.InnerText = whseid;
                sku_result.AppendChild(whseidResult);

                XmlElement master_labelResult = docreturn.CreateElement("master_label");
                master_labelResult.InnerText = master_label;
                sku_result.AppendChild(master_labelResult);

                XmlElement skuResult = docreturn.CreateElement("sku");
                skuResult.InnerText = sku;
                sku_result.AppendChild(skuResult);

                XmlElement batchResult = docreturn.CreateElement("batch");
                batchResult.InnerText = batch;
                sku_result.AppendChild(batchResult);

                XmlElement label_qtyResult = docreturn.CreateElement("label_qty");
                label_qtyResult.InnerText = label_qty;
                sku_result.AppendChild(label_qtyResult);

                XmlElement qtyResult = docreturn.CreateElement("qty");
                qtyResult.InnerText = qty;
                sku_result.AppendChild(qtyResult);

                XmlElement return_statusResult = docreturn.CreateElement("return_status");
                return_statusResult.InnerText = return_status;
                sku_result.AppendChild(return_statusResult);

                rootResult.AppendChild(sku_result);
                #endregion
            }
            catch (Exception ex)
            {
                App.Log("PDAXmlPrePickProcess.cs-1", ex.Message.ToString());
            }
            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;
        }
    }
}
